package cn.technology.forum.service.Impl;

import cn.technology.forum.mapper.QuestionExtMapper;
import cn.technology.forum.mapper.QuestionMapper;
import cn.technology.forum.model.Question;
import cn.technology.forum.model.QuestionExample;
import cn.technology.forum.service.QuestionManagerService;
import cn.technology.forum.vo.PageVO;
import cn.technology.forum.vo.QuestionVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zhangyukang
 * @Date 2020/3/12 11:06
 * @Version 1.0
 **/
@Service
public class QuestionManagerServiceImpl implements QuestionManagerService {

    @Autowired
    private QuestionMapper questionMapper;

    @Resource
    private QuestionExtMapper questionExtMapper;


    @Override
    public PageVO<Question> list(QuestionVO QuestionVO) {
        PageHelper.startPage(QuestionVO.getPage(),QuestionVO.getLimit());
        QuestionExample example = new QuestionExample();

        example.setOrderByClause("gmt_create desc");
        if(QuestionVO.getTitle()!=null&&!"".equalsIgnoreCase(QuestionVO.getTitle())){
            example.createCriteria().andTitleLike("%"+QuestionVO.getTitle()+"%");
        }
        if(QuestionVO.getTag()!=null&&!"".equals(QuestionVO.getTag())){
            example.createCriteria().andTagLike("%"+QuestionVO.getTag()+"%");
        }

        List<Question> questions = questionMapper.selectByExample(example);

        PageInfo<Question> pageInfo = new PageInfo<>(questions);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }
    @Override
    public void delete(Long id) {
        questionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateTop(QuestionVO questionVO) {

    }

}
